﻿using Microsoft.Web.WebView2.WinForms;
using Newtonsoft.Json;
using System.Diagnostics.Metrics;
using System;
using System.IO;
using System.Net;
using System.Net.Http.Json;
using System.Reflection;
using System.Text.Json.Serialization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using System.Drawing;

namespace CL_Sales
{
    public partial class Form1 : Form
    {
        CookieContainer cookieContainer = new CookieContainer();
        public Form1()
        {
            InitializeComponent();
            webView21.SourceChanged += WebView21_SourceChanged;
        }

        private void WebView21_SourceChanged(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2SourceChangedEventArgs e)
        {
            if (webView21.Source == new Uri("https://boss.mcdonalds.cn/mcd-boss/home"))
            {
                webView21.CoreWebView2.Navigate("https://boss.mcdonalds.cn/oap/report-data-details?id=17942648879079064");
            }
        }

        private async void Form1_Load(object sender, EventArgs e)
        {
            await webView21.EnsureCoreWebView2Async();
            this.Resize += Form1_Resize;
            var list = new List<DayPartItem>()
            {
                new DayPartItem(){ Name = "Breakfast",Value ="Breakfast",Value2 ="Breakfast",Xml ="<sawx:expr xmlns:sawx=\"com.siebel.analytics.web/expression/v1.1\" xsi:type=\"sawx:list\" op=\"in\" emptyAsAllChoices=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><sawx:expr xsi:type=\"sawx:sqlExpression\">\"Daypart\".\"Hourly\"</sawx:expr><sawx:expr xsi:type=\"xsd:string\">05:00-06:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">06:00-07:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">07:00-08:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">08:00-09:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">09:00-10:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">10:00-10:30</sawx:expr></sawx:expr>"},
                new DayPartItem(){ Name="Lunch",Value="Lunch",Value2="Lunch",Xml="<sawx:expr xmlns:sawx=\"com.siebel.analytics.web/expression/v1.1\" xsi:type=\"sawx:list\" op=\"in\" emptyAsAllChoices=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><sawx:expr xsi:type=\"sawx:sqlExpression\">\"Daypart\".\"Hourly\"</sawx:expr><sawx:expr xsi:type=\"xsd:string\">10:30-11:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">11:00-12:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">12:00-13:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">13:00-14:00</sawx:expr></sawx:expr>"},
                new DayPartItem(){ Name ="Tea Time",Value ="Tea%20Time",Value2 ="Tea Time", Xml = "<sawx:expr xmlns:sawx=\"com.siebel.analytics.web/expression/v1.1\" xsi:type=\"sawx:list\" op=\"in\" emptyAsAllChoices=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><sawx:expr xsi:type=\"sawx:sqlExpression\">\"Daypart\".\"Hourly\"</sawx:expr><sawx:expr xsi:type=\"xsd:string\">14:00-15:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">15:00-16:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">16:00-17:00</sawx:expr></sawx:expr>"},
                new DayPartItem(){ Name ="Dinner",Value ="Dinner",Value2="Dinner", Xml = "<sawx:expr xmlns:sawx=\"com.siebel.analytics.web/expression/v1.1\" xsi:type=\"sawx:list\" op=\"in\" emptyAsAllChoices=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><sawx:expr xsi:type=\"sawx:sqlExpression\">\"Daypart\".\"Hourly\"</sawx:expr><sawx:expr xsi:type=\"xsd:string\">17:00-18:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">18:00-19:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">19:00-20:00</sawx:expr></sawx:expr>"},
                new DayPartItem(){ Name ="Night Snack",Value ="Night%20Snack",Value2 ="Night Snack", Xml = "<sawx:expr xmlns:sawx=\"com.siebel.analytics.web/expression/v1.1\" xsi:type=\"sawx:list\" op=\"in\" emptyAsAllChoices=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><sawx:expr xsi:type=\"sawx:sqlExpression\">\"Daypart\".\"Hourly\"</sawx:expr><sawx:expr xsi:type=\"xsd:string\">20:00-21:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">21:00-22:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">22:00-23:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">23:00-24:00</sawx:expr></sawx:expr>"},
                new DayPartItem(){ Name ="After Midnight",Value ="After%20Midnight",Value2 = "After Midnight", Xml = "<sawx:expr xmlns:sawx=\"com.siebel.analytics.web/expression/v1.1\" xsi:type=\"sawx:list\" op=\"in\" emptyAsAllChoices=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><sawx:expr xsi:type=\"sawx:sqlExpression\">\"Daypart\".\"Hourly\"</sawx:expr><sawx:expr xsi:type=\"xsd:string\">00:00-01:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">01:00-02:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">02:00-03:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">03:00-04:00</sawx:expr><sawx:expr xsi:type=\"xsd:string\">04:00-05:00</sawx:expr></sawx:expr>"}
            };
            comboBox1.DataSource = list;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Value";
        }

        private void Form1_Resize(object? sender, EventArgs e)
        {
            this.webView21.Size = this.ClientSize - new System.Drawing.Size(webView21.Location);
        }

        private async void btnGetSales_Click(object sender, EventArgs e)
        {
            string? path;
            int waittime = Convert.ToInt32(textBox1.Text);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.RestoreDirectory = true;
            sfd.Filter = "*.xlsx|Excel file";
            sfd.DefaultExt = ".xlsx";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
            else
            {
                return;
            }
            //开始获取cookies
            // 获取当前浏览网页的Cookies
            var cookieManager = webView21.CoreWebView2.CookieManager;
            var cookies = await cookieManager.GetCookiesAsync("https://biee.mcdonalds.cn/McclBISys");
            //开始获取数据
            var handler = new HttpClientHandler() { AllowAutoRedirect = false, UseCookies = true };
            using (var client = new HttpClient(handler))
            {
                if (cookies.Count == 0)
                {
                    MessageBox.Show("Cookies不能为空!");
                    return;
                }
                else
                {
                    foreach (var cookie in cookies)
                    {
                        handler.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
                    }
                }
                RequestCLSalesData requestCLSalesData;
                if (textBox2.Text.Length > 0)
                {
                    string p2 = "\"Operation\".\"Store Key\"";
                    string p3 = "1 \"" + textBox2.Text + "\"";
                    string p8 = "\"Operation\".\"Store\"";
                    string p9 = "1 \"1420411 MAOMING YOUCHENG TEN\"";
                    string page = "R07.Daily Sales Rpt by Period And Store";
                    requestCLSalesData = new RequestCLSalesData(client, waittime, p2, p3, p8, p9, page);
                }
                else
                {
                    requestCLSalesData = new RequestCLSalesData(client, waittime);
                }
                HttpResponseMessage? response = await requestCLSalesData.GetSalesData(dateTimePicker1.Value, dateTimePicker2.Value);
                Stream stream = await response.Content.ReadAsStreamAsync();
                using (FileStream fileStream = new FileStream(path, FileMode.Create))
                {
                    stream.CopyTo(fileStream);
                }
                MessageBox.Show("输出成功!");
            }
        }

        private async void btnGetPeakHour_Click(object sender, EventArgs e)
        {
            string? path;
            int waittime = Convert.ToInt32(textBox1.Text);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.RestoreDirectory = true;
            sfd.Filter = "*.xlsx|Excel file";
            sfd.DefaultExt = ".xlsx";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
            else
            {
                return;
            }
            //开始获取cookies
            // 获取当前浏览网页的Cookies
            var cookieManager = webView21.CoreWebView2.CookieManager;
            var cookies = await cookieManager.GetCookiesAsync("https://biee.mcdonalds.cn/McclBISys");
            //开始获取数据
            var handler = new HttpClientHandler() { AllowAutoRedirect = false, UseCookies = true };
            using (var client = new HttpClient(handler))
            {
                if (cookies.Count == 0)
                {
                    MessageBox.Show("Cookies不能为空!");
                    return;
                }
                else
                {
                    foreach (var cookie in cookies)
                    {
                        handler.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
                    }
                }
                DayPartItem dayPart = (DayPartItem)comboBox1.SelectedItem;
                RequestCLPeakHourData requestCLPeakHourData = new RequestCLPeakHourData(client, waittime, dayPart);
                HttpResponseMessage? response = await requestCLPeakHourData.GetPeakHourData(dateTimePicker1.Value, dateTimePicker2.Value);
                Stream stream = await response.Content.ReadAsStreamAsync();
                using (FileStream fileStream = new FileStream(path, FileMode.Create))
                {
                    stream.CopyTo(fileStream);
                }
                MessageBox.Show("输出成功!");
            }
        }

        private async void btnBatchGetSales_Click(object sender, EventArgs e)
        {
            string? path;
            int waittime = Convert.ToInt32(textBox1.Text);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.RestoreDirectory = true;
            sfd.Filter = "*.xlsx|Excel file";
            sfd.DefaultExt = ".xlsx";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
            else
            {
                return;
            }
            //开始获取cookies
            // 获取当前浏览网页的Cookies
            var cookieManager = webView21.CoreWebView2.CookieManager;
            var cookies = await cookieManager.GetCookiesAsync("https://biee.mcdonalds.cn/McclBISys");
            //开始获取数据
            var handler = new HttpClientHandler() { AllowAutoRedirect = false, UseCookies = true };
            using (var client = new HttpClient(handler))
            {
                if (cookies.Count == 0)
                {
                    MessageBox.Show("Cookies不能为空!");
                    return;
                }
                else
                {
                    foreach (var cookie in cookies)
                    {
                        handler.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
                    }
                }
                string P3 = File.ReadAllText("list.txt");
                string[] list = P3.Split('，');
                string P9 = string.Empty;
                if (list.Length > 5)
                {
                    P9 = "5 " + list[0] + " " + list[1] + " " + list[2] + " " + list[3] + " " + list[4];
                }
                else
                {
                    P9 = list.Length.ToString();
                    foreach (string str in list)
                    {
                        P9 += " ";
                        P9 += str;
                    }
                }
                P3 = list.Length.ToString();
                foreach (string str in list)
                {
                    P3 += " ";
                    P3 += str;
                }
                RequestBatchCLSalesData requestBatchCLSalesData = new RequestBatchCLSalesData(client, waittime, P3, P9);
                HttpResponseMessage? response = await requestBatchCLSalesData.GetSalesData(dateTimePicker1.Value, dateTimePicker2.Value);
                Stream stream = await response.Content.ReadAsStreamAsync();
                using (FileStream fileStream = new FileStream(path, FileMode.Create))
                {
                    stream.CopyTo(fileStream);
                }
                MessageBox.Show("输出成功!");
            }
        }

        private async void btnBatchGetSales2_Click(object sender, EventArgs e)
        {
            string? path;
            int waittime = Convert.ToInt32(textBox1.Text);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.RestoreDirectory = true;
            sfd.Filter = "*.xlsx|Excel file";
            sfd.DefaultExt = ".xlsx";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
            else
            {
                return;
            }
            //开始获取cookies
            // 获取当前浏览网页的Cookies
            var cookieManager = webView21.CoreWebView2.CookieManager;
            var cookies = await cookieManager.GetCookiesAsync("https://biee.mcdonalds.cn/McclBISys");
            //开始获取数据
            var handler = new HttpClientHandler() { AllowAutoRedirect = false, UseCookies = true };
            using (var client = new HttpClient(handler))
            {
                if (cookies.Count == 0)
                {
                    MessageBox.Show("Cookies不能为空!");
                    return;
                }
                else
                {
                    foreach (var cookie in cookies)
                    {
                        handler.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
                    }
                }
                string P3 = File.ReadAllText("list2.txt");
                string[] list = P3.Split('，');
                string P9 = string.Empty;
                if (list.Length > 5)
                {
                    P9 = "5 " + list[0] + " " + list[1] + " " + list[2] + " " + list[3] + " " + list[4];
                }
                else
                {
                    P9 = list.Length.ToString();
                    foreach (string str in list)
                    {
                        P9 += " ";
                        P9 += str;
                    }
                }
                P3 = list.Length.ToString();
                foreach (string str in list)
                {
                    P3 += " ";
                    P3 += str;
                }
                RequestBatchCLSalesData requestBatchCLSalesData = new RequestBatchCLSalesData(client, waittime, P3, P9);
                HttpResponseMessage? response = await requestBatchCLSalesData.GetSalesData(dateTimePicker1.Value, dateTimePicker2.Value);
                Stream stream = await response.Content.ReadAsStreamAsync();
                using (FileStream fileStream = new FileStream(path, FileMode.Create))
                {
                    stream.CopyTo(fileStream);
                }
                MessageBox.Show("输出成功!");
            }
        }

        private async void btnGetBreakfast_Click(object sender, EventArgs e)
        {
            string? path;
            int waittime = Convert.ToInt32(textBox1.Text);
            FolderBrowserDialog sfd = new FolderBrowserDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.SelectedPath;
            }
            else
            {
                return;
            }
            //开始获取cookies
            // 获取当前浏览网页的Cookies
            var cookieManager = webView21.CoreWebView2.CookieManager;
            var cookies = await cookieManager.GetCookiesAsync("https://biee.mcdonalds.cn/McclBISys");
            //开始获取数据
            var handler = new HttpClientHandler() { AllowAutoRedirect = false, UseCookies = true };
            using (var client = new HttpClient(handler))
            {
                if (cookies.Count == 0)
                {
                    MessageBox.Show("Cookies不能为空!");
                    return;
                }
                else
                {
                    foreach (var cookie in cookies)
                    {
                        handler.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
                    }
                }
            }
        }
    }
}